package com.pangolin.pangolinim.ui.adapter;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.pangolin.pangolinim.R;
import com.pangolin.pangolinim.ui.activity.UserInfoActivity;
import com.pangolin.pangolinim.utils.GsonUtil;

import java.util.List;

public class ContactsAdapter extends RecyclerView.Adapter<ContactsAdapter.ViewHolder>{
    private List<UIContact> mContactsList;
    private UIContact me;

    public ContactsAdapter(List<UIContact> contactsList , UIContact me){
        mContactsList = contactsList;
        this.me = me;
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView ivHeader;
        TextView tvName;
        LinearLayout llContact;
        public ViewHolder(View view){
            super(view);
            tvName = view.findViewById(R.id.tvName);
            ivHeader = view.findViewById(R.id.ivHeader);
            llContact = view.findViewById(R.id.llcontact);
        }
    }

    @Override
    public ContactsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_contact , parent , false);
        final ContactsAdapter.ViewHolder holder = new ContactsAdapter.ViewHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        UIContact contact = mContactsList.get(holder.getAbsoluteAdapterPosition());
        //设置昵称
        if(contact.getName() != null){
            holder.tvName.setText(contact.getName());
        }
        //设置头像
        if (contact.getBitHeader() != null){
            holder.ivHeader.setImageBitmap(contact.getBitHeader());
        }
        //点击响应
        holder.llContact.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(view.getContext(), UserInfoActivity.class);
                intent.putExtra("param1", GsonUtil.toJson(me));
                intent.putExtra("param2", GsonUtil.toJson(contact));
                view.getContext().startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount(){
        return mContactsList.size();
    }
}
